/*
 * SVPAboutBox.java
 */

package svp;

import java.awt.Cursor;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import javax.mail.MessagingException;

public class SVPAboutBox extends javax.swing.JDialog {

    public SVPAboutBox(java.awt.Frame parent) {
        super(parent);
        initComponents();
       
       toTF.setVisible(false);
       jLabel1.setVisible(false);
       ccTF.setVisible(false);
       jLabel2.setVisible(false);
       
        getRootPane().setDefaultButton(closeButton);
    }

    @Action public void closeAboutBox() {
        Calendar c = Calendar.getInstance();
        subjectTF.setText("Reporting Issue on " + c.getTime().toGMTString());
        attachmentTF.setText("");
        messageTA.setText("");
        dispose();
    }
    
    @Action public void sendReport() {
        
    toAddress = toTF.getText().trim();
        ccAddress = ccTF.getText().trim();
        subject = subjectTF.getText().trim();
        message = messageTA.getText().trim();
        //this.message = message;
        String attachment = attachmentTF.getText() + " ";

        attachments = attachment.split(" ");

        receipients = toAddress + "," + ccAddress;
        receipientsList = receipients.split(",");

        SendMailUsingAuthentication mailUsingAuthentication =
                new SendMailUsingAuthentication();
        try {
             
            mailUsingAuthentication.postMail(receipientsList,
                    subject, message, fromAddress, authenticationPassword, attachments);
            jLabel7.setText("A error was Successfully reported on " + Calendar.getInstance().getTime().toGMTString());
            closeAboutBox();
        } catch (MessagingException messagingException) {
            messagingException.printStackTrace();
            jLabel7.setText(messagingException.getMessage());
        }  
    
      
    
    }
    public  String sendReport(Exception e) {
        
    toAddress = toTF.getText().trim();
        ccAddress = ccTF.getText().trim();
        subject = Calendar.getInstance().getTime().toGMTString();
        message = "";
        StackTraceElement[] ste = e.getStackTrace();
       for(StackTraceElement s: ste){
       message = message + "\nError : "+ e.toString() + "\nClass Name : " + s.getClassName()+ "\nMethod Name : " + s.getMethodName()+"\nLine Number : "+ s.getLineNumber();
       }  
        String attachment = attachmentTF.getText() + " ";

        attachments = attachment.split(" ");

        receipients = toAddress + "," + ccAddress;
        receipientsList = receipients.split(",");

        SendMailUsingAuthentication mailUsingAuthentication =
                new SendMailUsingAuthentication();
        try {
//            System.out.println(receipientsList); 
//            System.out.println(subject);
//            System.out.println(message);
//            System.out.println(authenticationPassword);
            
            mailUsingAuthentication.postMail(receipientsList,
                    subject, message, fromAddress, authenticationPassword, attachments);
            return " An error has been successfully reported to the development team on  " + Calendar.getInstance().getTime().toGMTString() + ".\n Someone from the development team would be assisting you shortly.!";
           
        } catch (MessagingException messagingException) {
            //messagingException.printStackTrace();
            return messagingException.getMessage();
        }  
    
      
    
    }
    @Action void login(String userName, String password)
    {
        fromAddress = userName;
        authenticationPassword = password;
        System.out.println("User name : " + fromAddress);
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selectFileFC = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        closeButton = new javax.swing.JButton();
        reportErrorBT = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        toTF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        ccTF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        subjectTF = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        attachmentTF = new javax.swing.JTextField();
        attachFileBT = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        messageTA = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();

        selectFileFC.setName("selectFileFC"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(svp.SVPApp.class).getContext().getResourceMap(SVPAboutBox.class);
        setTitle(resourceMap.getString("title")); // NOI18N
        setModal(true);
        setName("aboutBox"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(svp.SVPApp.class).getContext().getActionMap(SVPAboutBox.class, this);
        closeButton.setAction(actionMap.get("closeAboutBox")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N

        reportErrorBT.setText(resourceMap.getString("reportErrorBT.text")); // NOI18N
        reportErrorBT.setName("reportErrorBT"); // NOI18N
        reportErrorBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reportErrorBTActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        toTF.setText(resourceMap.getString("toTF.text")); // NOI18N
        toTF.setName("toTF"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        ccTF.setText(resourceMap.getString("ccTF.text")); // NOI18N
        ccTF.setName("ccTF"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        subjectTF.setEditable(false);
        subjectTF.setText(resourceMap.getString("subjectTF.text")); // NOI18N
        subjectTF.setName("subjectTF"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        attachmentTF.setText(resourceMap.getString("attachmentTF.text")); // NOI18N
        attachmentTF.setName("attachmentTF"); // NOI18N

        attachFileBT.setText(resourceMap.getString("attachFileBT.text")); // NOI18N
        attachFileBT.setName("attachFileBT"); // NOI18N
        attachFileBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                attachFileBTActionPerformed(evt);
            }
        });

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        messageTA.setColumns(20);
        messageTA.setFont(resourceMap.getFont("messageTA.font")); // NOI18N
        messageTA.setLineWrap(true);
        messageTA.setRows(5);
        messageTA.setWrapStyleWord(true);
        messageTA.setName("messageTA"); // NOI18N
        jScrollPane1.setViewportView(messageTA);

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(subjectTF, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                            .addComponent(toTF, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                            .addComponent(ccTF, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(attachmentTF, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(attachFileBT))
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(91, 91, 91))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(327, Short.MAX_VALUE)
                .addComponent(reportErrorBT)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(closeButton)
                .addGap(85, 85, 85))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(toTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ccTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(subjectTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(attachmentTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(attachFileBT))
                .addGap(3, 3, 3)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(reportErrorBT)
                    .addComponent(closeButton))
                .addGap(64, 64, 64))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setName("jPanel2"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel7)
                .addContainerGap(510, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel7)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 455, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void attachFileBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_attachFileBTActionPerformed
// TODO add your handling code here:
        selectFileFC.showOpenDialog(this);
        attachmentTF.setText(selectFileFC.getSelectedFile().toString());
}//GEN-LAST:event_attachFileBTActionPerformed

private void reportErrorBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reportErrorBTActionPerformed
// TODO add your handling code here:
   
    jLabel7.setText("Please wait..!! Your report is being sent");
    jLabel7.validate();
    if(jLabel7.getText().equalsIgnoreCase("Please wait..!! Your report is being sent"))
    jPanel1.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));   
    sendReport();
    jPanel1.setCursor(Cursor.getDefaultCursor()); 
}//GEN-LAST:event_reportErrorBTActionPerformed

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
// TODO add your handling code here:
  Calendar c = Calendar.getInstance();
  subjectTF.setText("Reporting Issue on " + c.getTime().toGMTString());   
}//GEN-LAST:event_formWindowOpened

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
    
}//GEN-LAST:event_formWindowClosing
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton attachFileBT;
    private javax.swing.JTextField attachmentTF;
    private javax.swing.JTextField ccTF;
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    public javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea messageTA;
    private javax.swing.JButton reportErrorBT;
    private javax.swing.JFileChooser selectFileFC;
    private javax.swing.JTextField subjectTF;
    private javax.swing.JTextField toTF;
    // End of variables declaration//GEN-END:variables
    String toAddress = null, ccAddress = null, message = null,
    receipientsList[] = null, attachments[] = null, receipients, subject;
    String fromAddress = "srivigneshwarapaints@gmail.com"; //Place your mail id here.
    String authenticationPassword = "welcomesvp";//Place your password here
}
